<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=1.1&services=true"></script>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <!-- 引入组件库 -->
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
        <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=nSxiPohfziUaCuONe4ViUP2N"></script>
        <style>
            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }
            .datatable td, .datatable th {
                padding: 12px 0;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: left;
            }
             .avatar-uploader .el-upload {
                 border: 1px dashed #d9d9d9;
                 border-radius: 6px;
                 cursor: pointer;
                 position: relative;
                 overflow: hidden;
             }
            .avatar-uploader .el-upload:hover {
                border-color: #409EFF;
            }
            .avatar-uploader-icon {
                font-size: 28px;
                color: #8c939d;
                width: 178px;
                height: 178px;
                line-height: 178px;
                text-align: center;
            }
            .avatar {
                width: 178px;
                height: 178px;
                display: block;
            }


        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>后管体检机构地址管理<small>体检机构</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>预约管理</el-breadcrumb-item>
                    <el-breadcrumb-item>检查组管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="机构地址" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        <el-button @click="pagination.currentPage=1; findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="体检机构地址" align="center"></el-table-column>
                        <el-table-column prop="longitude" label="经度" align="center"></el-table-column>
                        <el-table-column prop="latitude" label="纬度" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增体检机构" :visible.sync="dialogFormVisible">
                            <template>
                                <el-tabs  v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form ref="dataAddForm" :rules="rules" :model="formData" label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="体检机构地址" prop="name">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="上传图片">
                                                        <el-upload
                                                                class="avatar-uploader"
                                                                action="/address/upload.do"
                                                                :auto-upload="autoUpload"
                                                                name="imgFile"
                                                                :show-file-list="false"
                                                                :on-success="handleAvatarSuccess"
                                                                :before-upload="beforeAvatarUpload">
                                                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                                        </el-upload>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                    <el-container >
                                        <el-aside style="margin-top:15px">
                                            <el-table :data="buildinglist" @row-click="skiptomap" fit highlight-current-row stripe border >
                                                <el-table-column prop="name" v-model="name"  lable="name" align="center" ></el-table-column>
                                            </el-table>
                                        </el-aside>
                                        <el-container id="newmap" style="margin-top:20px;margin-left:20px; height:400px;"></el-container>
                                    </el-container>
                                </el-tabs>

                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑检查组" :visible.sync="dialogFormVisible4Edit">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form ref="dataEditForm" :rules="rules" :model="formData" label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="体检机构地址" prop="name">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                                <!--<el-col :span="12">-->
                                                    <!--<el-form-item label="经度" prop="longitude">-->
                                                        <!--<el-input v-model="formData.longitude"/>-->
                                                    <!--</el-form-item>-->
                                                <!--</el-col>-->
                                            </el-row>
                                            <!--<el-row>-->
                                                <!--<el-col :span="12">-->
                                                    <!--<el-form-item label="纬度">-->
                                                        <!--<el-input v-model="formData.latitude"/>-->
                                                    <!--</el-form-item>-->
                                                <!--</el-col>-->
                                            <!--</el-row>-->
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="上传图片">
                                                        <el-upload
                                                                class="avatar-uploader"
                                                                action="/address/upload.do"
                                                                :auto-upload="autoUpload"
                                                                name="imgFile"
                                                                :show-file-list="false"
                                                                :on-success="handleAvatarSuccess"
                                                                :before-upload="beforeAvatarUpload">
                                                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                                        </el-upload>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                    <el-container >
                                        <el-aside style="margin-top:15px">
                                            <el-table :data="buildinglist" @row-click="skiptomap" fit highlight-current-row stripe border >
                                                <el-table-column prop="name" v-model="name"  lable="name" align="center" ></el-table-column>
                                            </el-table>
                                        </el-aside>
                                        <el-container id="newmap" style="margin-top:20px;margin-left:20px; height:400px;"></el-container>
                                    </el-container>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>


    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                activeName:'first',//添加/编辑窗口Tab标签名称
                imageUrl:null,
                pagination: {//分页相关属性
                    currentPage: 1,
                    pageSize:10,
                    total:100,
                    queryString:null,
                },
                dataList: [],//列表数据
                formData: {
                    longitude:"",
                    latitude:""
                },//表单数据
                dialogFormVisible: false,//控制添加窗口显示/隐藏
                dialogFormVisible4Edit:false,//控制编辑窗口显示/隐藏
                //定义指定地点的名称和经纬度
                buildinglist: [{ name: '厦门勇富体检中心', j: 118.094566, w: 24.457814 },
                    { name: '厦门黑马体检中心', j: 118.125996, w: 24.480687 },
                    { name: '厦门市儿童医院体检中心', j: 118.118713, w: 24.511845 },
                    { name: '厦门仙岳体检中心', j: 118.118282, w: 24.501587 },
                    { name: '厦门大学医院体检中心', j: 118.097674, w: 24.44178 },
                    { name: '厦门中山医院', j: 118.10532, w: 24.477346 },
                    { name: '厦门市中医院体检中心', j: 118.146819, w: 24.505877 }],
                jd:118.10532,  //初始地图中心点的经纬度
                wd:24.477346,
                map: {},
                point: {},
                rules: {//校验规则
                    name:[{ required: true, message: '体检机构地址为必填项', trigger: 'blur' }]
                },
            },
            created() {
                this.findPage();
            },
            methods: {
                //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
                handleAvatarSuccess(response, file) {
                    this.imageUrl="http://q1plri7b7.bkt.clouddn.com/"+response.data
                    this.$message({
                        message:response.message,
                        type:response.flag? "success":"error"
                    });
                    this.formData.img = response.data;
                },
                //上传图片之前执行
                beforeAvatarUpload(file) {
                    const isJPG = file.type === 'image/jpeg';
                    const isLt2M = file.size / 1024 / 1024 < 2;
                    if (!isJPG) {
                        this.$message.error('上传套餐图片只能是 JPG 格式!');
                    }
                    if (!isLt2M) {
                        this.$message.error('上传套餐图片大小不能超过 2MB!');
                    }
                    return isJPG && isLt2M;
                },
                loadmap(jd, wd) {
                    var _this = this;   //保存此时的this值！！！
                    this.map = new BMap.Map("newmap");          // 创建地图实例
                    this.point = new BMap.Point(jd, wd);  // 创建点坐标
                    this.map.centerAndZoom(this.point, 12);                 // 初始化地图，设置中心点坐标和地图级别
                    this.map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
                    this.buildinglist.forEach(function (item) {    //创建多标注
                        var point2 = new BMap.Point(item.j, item.w);
                        var marker = new BMap.Marker(point2);        // 创建标注
                        _this.map.addOverlay(marker);                    // 将标注添加到地图中
                    })
                },
                skiptomap(row, event, column) {
                    this.formData.longitude=row.j
                    this.formData.latitude=row.w
                    this.point = new BMap.Point(row.j, row.w);
                    this.map.centerAndZoom(this.point, 14);
                    var infoWindow = new BMap.InfoWindow(row.name);  // 创建信息窗口对象
                    this.map.openInfoWindow(infoWindow, this.point); //开启信息窗口
                },
                //编辑
                handleEdit() {
                    this.$refs['dataEditForm'].validate((valid)=>{
                if(valid){
                    axios.post("/address/update.do?",this.formData).then((res)=>{
                        if(res.data.flag){
                            this.$message.success(res.data.message)
                            this.dialogFormVisible4Edit=false
                            this.findPage()
                        }else {
                            this.$message.error(res.data.message)
                        }
                    }).catch((err)=>{alert(error)})
                        }
                    })
                },
                //添加
                handleAdd () {

                    this.$refs['dataAddForm'].validate((valid)=>{
                        if(valid){
                            //校验成功
                    axios.post("/address/add.do",this.formData).then((res)=>{
                        if(res.data.flag){
                            this.$message.success(res.data.message)
                            this.dialogFormVisible=false
                            this.findPage()
                        }else {
                            this.$message.error(res.data.message)
                        }
                    }).catch((error)=>{alert(error)})
                        }else {
                            //校验失败
                            this.$message.error("表单校验失败，请重新输入")
                            return false;
                        }
                    })
                },
                //分页查询
                findPage() {
                    var parma={
                        currentPage: this.pagination.currentPage,
                        pageSize: this.pagination.pageSize,
                        queryString:this.pagination.queryString
                    }
                    // if(parma.queryString!=null){
                    //     parma.currentPage=1
                    // }
                    axios.post("/address/findPage.do",parma).then((res)=>{
                        this.dataList = res.data.rows
                        this.pagination.total=res.data.total
                    }).catch((error)=>{alert(error)})
                },
                // 重置表单
                resetForm() {
                    this.formData= {}
                },
                // 弹出添加窗口
                handleCreate() {
                    this.imageUrl=null,
                    this.resetForm()
                    this.dialogFormVisible=true
                    this.loadmap(this.jd,this.wd);
                },
                // 弹出编辑窗口
                handleUpdate(row) {
                    this.imageUrl=null,
                    this.dialogFormVisible4Edit=true
                    this.loadmap(this.jd,this.wd);
                    //查询体检机构地址，并进行回显
                    axios.get("/address/findById.do?id="+row.id).then((res)=>{
                       if(res.data.flag){
                           this.formData = res.data.data
                       }else {
                           this.$message.error(res.data.message)
                       }
                    }).catch((error)=>{alert(error)})
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage=currentPage
                    this.findPage()
                },
                // 删除
                handleDelete(row) {
                    this.$confirm("您确定删除该条记录?","提示",{type:"warning"}).then(()=>{
                           axios.get("/address/delete.do?id="+row.id).then((res)=>{
                               if(res.data.flag){
                                   this.$message.success(res.data.message)
                               }else {
                                   this.$message.error(res.data.message)
                               }
                           }).catch((error)=>{alert(error)}).finally(()=>{ this.findPage()})
                    }).catch(()=>{
                        this.$message.info("您已取消删除！！！")
                    })
                }
            }

        })

    </script>
</html>
